package com.shoeHouse.order.service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.shoeHouse.order.dao.OrderInfoMapper;
import com.shoeHouse.order.model.OrderInfo;
import com.shoeHouse.order.model.OrderInfoExample;
import com.shoeHouse.order.model.OrderInfoExample.Criteria;
import com.shoeHouse.util.PageHelper;

@Service
public class OrderServiceImpl implements OrderService{
	@Autowired
	private OrderInfoMapper orderInfoMapper;
	

	@Override
	public void save(OrderInfo o) {
		// TODO Auto-generated method stub
		
		orderInfoMapper.insert(o);
		
	}

	@Override
	public void deleteAll(String ids) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void removeAll(String ids) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void update(OrderInfo o) {
		// TODO Auto-generated method stub
	/*	OrderInfoExample example=new OrderInfoExample();
		Criteria criteria=example.createCriteria();*/
	/*	criteria.andIdEqualTo(o.getId());*/
		/*o.setId(null);*/
	//	System.out.println(o.getId()+"####"+o.getPayStatus());
		orderInfoMapper.updateByPrimaryKeySelective(o);
	}

	@Override
	public int count() {
		// TODO Auto-generated method stub
		OrderInfoExample example=new OrderInfoExample();
		Criteria criteria=example.createCriteria();
		criteria.andStatusEqualTo(Byte.parseByte("0"));
		int count=orderInfoMapper.countByExample(example);
		return count;
	}

	@Override
	public List<OrderInfo> getAll(HttpServletRequest request,OrderInfo orderInfo, PageHelper pageHelper) {
		// TODO Auto-generated method stub
		OrderInfoExample example=new OrderInfoExample();
		Criteria criteria=example.createCriteria();
		criteria.andStatusEqualTo(Byte.parseByte("0"));
		String limit="limit "+(pageHelper.getCurrentPage()-1)*pageHelper.getPageSize()+","+pageHelper.getPageSize();
		example.setOrderByClause("gmt_modified DESC "+limit);
		if(null!=orderInfo.getPayStatus()&&!orderInfo.getPayStatus().equals("")){
			criteria.andPayStatusEqualTo(orderInfo.getPayStatus());
			
		}
		if(null!=orderInfo.getId()&&!orderInfo.getId().equals("")){
			criteria.andIdEqualTo(orderInfo.getId());
			
		}
		if(null!=orderInfo.getUserId()&&!orderInfo.getUserId().equals("")){
			criteria.andUserIdEqualTo(orderInfo.getUserId());
			
		}
		if(null!=orderInfo.getSeller()&&!orderInfo.getSeller().equals("")){
			criteria.andSellerEqualTo(orderInfo.getSeller());
			
		}
		String endTime=request.getParameter("endTime");
		String beginTime=request.getParameter("beginTime");
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");  
		if(beginTime!=null){
			if(endTime!=null){
				try {
					criteria.andGmtModifiedBetween(sdf.parse(beginTime),sdf.parse(endTime));
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}else{
				try {
					criteria.andGmtModifiedBetween(sdf.parse(beginTime),new Date());
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			}
		}
		
		
		List<OrderInfo> list=orderInfoMapper.selectByExample(example);
		return list;
	}

	@Override
	public OrderInfo getSingle(OrderInfo orderInfo) {
		// TODO Auto-generated method stub
		OrderInfoExample example=new OrderInfoExample();
		Criteria criteria=example.createCriteria();
		criteria.andStatusEqualTo(Byte.parseByte("0"));
		if(null!=orderInfo.getUserId()&&!orderInfo.getUserId().equals("")){
			criteria.andUserIdEqualTo(orderInfo.getUserId());
			
		}
		OrderInfo info=orderInfoMapper.selectByPrimaryKey(orderInfo.getId());
		return info;
	}

}
